package com.manager.core.module.manager.service;

import com.manager.commons.encrypt.md5.MD5;
import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.commons.mybatis.service.GenericService;
import com.manager.commons.utils.DateUtil;
import com.manager.core.web.json.AjaxJson;
import com.manager.enterprise.module.entity.DateLoginUser;
import com.manager.enterprise.module.entity.FileDataInfo;
import com.manager.enterprise.module.entity.FileTotalNumber;
import com.manager.enterprise.module.platform.core.dao.PlatformUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MainService extends GenericService<String> {
    @Autowired
    private PlatformUserDao puDao;


    @Override
    public GenericDao<String> getDao() {
        return null;
    }

    @Override
    public String getModel() {
        return "";
    }

    public Map<String, Object> findAccountView(Map<String, Object> map) {
        return puDao.findAccountView(map);
    }

    public FileDataInfo getFileData(String creator) {
        FileDataInfo fileDataInfo = puDao.userNumber(creator);
        String currentData = DateUtil.currentDate();
        String currentDateThirty = DateUtil.currentDateThirty();
        List<Integer> dtl = new ArrayList<>();
        int maxDeathNumber = 5;
        List<String> dates = DateUtil.getDatesBetween(DateUtil.currentDateThirty(), DateUtil.currentDate());
        if (!dates.isEmpty() && dates.size() > 1) {
            dates.remove(dates.size() - 1);
        }
        fileDataInfo.setDatys(dates);
        if (fileDataInfo.getTotalUsers() > 0) {
            FileTotalNumber fileTotalNumber = puDao.fileTypeTotal(creator);
            fileDataInfo.setTotalFileNumber(fileTotalNumber.getTotalFileNumber());
            fileDataInfo.setImageNumber(fileTotalNumber.getImageNumber());
            fileDataInfo.setAudioNumber(fileTotalNumber.getAudioNumber());
            fileDataInfo.setVideoNumber(fileTotalNumber.getVideoNumber());
            fileDataInfo.setRarNumber(fileTotalNumber.getRarNumber());
            fileDataInfo.setDocNumber(fileTotalNumber.getDocNumber());
            Map<String, Object> parmas = new HashMap<>();
            parmas.put("startDate", currentDateThirty);
            parmas.put("endDate", currentData);
            parmas.put("creator", creator);
            List<DateLoginUser> deathNumberThirtyList = puDao.dateLoginUserNumber(parmas);
            for (String date : dates) {
                boolean isExist = false;
                for (DateLoginUser deathNumber : deathNumberThirtyList) {
                    if (date.equals(deathNumber.getEntryDate())) {
                        if (maxDeathNumber < deathNumber.getNumberDate()) {
                            maxDeathNumber = deathNumber.getNumberDate();
                        }
                        isExist = true;
                        dtl.add(deathNumber.getNumberDate());
                        deathNumberThirtyList.add(deathNumber);
                        break;
                    }
                }
                if (!isExist) {
                    dtl.add(0);
                }
            }

        }
        fileDataInfo.setDeathNumberList(dtl);
        fileDataInfo.setDeathNumberMax(maxDeathNumber);
        return fileDataInfo;
    }

    /**
     * 重置密码
     *
     * @param params
     * @return
     */
    public AjaxJson updatePwd(Map<String, Object> params) {
        if (null != params) {
            String oldPwd = getParamsString(params, "oldPwd");
            String newPwd = getParamsString(params, "newPwd");
            String opwd = MD5.md5(oldPwd);
            String oldPwdf = puDao.findPwd(params);
            if (!opwd.equals(oldPwdf)) {
                return new AjaxJson(0, "原密码输入错误");
            }
            String npwd = MD5.md5(newPwd);
            params.put("pwd", npwd);
            int f = puDao.updatePwd(params);
            return new AjaxJson(f, "密码修改" + ((f > 0) ? "成功" : "失败！"));
        }
        return new AjaxJson("参数错误");
    }
}
